﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using MedicService.Domain.Print;
using MedicService.Server.Factory.Generic;

namespace MedicService.Server.Factory.Print {
    public class SubstitutionFactory : EntityFactory<Substitution> {
        public SubstitutionFactory(string where) : base("Substitution", "Id", where) {}
        public SubstitutionFactory() : base("Substitution") { }

        protected override Substitution CreateEntity() {
            return new Substitution();
        }

        protected override string GetInsertQuery(Substitution entity) {
            string q = "INSERT INTO [Substitution] "
                       + " ([Alias]"
                       + " ,[Value]"
                       + " ,[Condition]"
                       + " ,[Context]"
                       + " ,[Replace])"
                       + " VALUES"
                       + " (" + Nullable(entity.Alias)
                       + " ," + Nullable(entity.Value)
                       + " ," + Nullable(entity.Condition)
                       + " ," + Nullable(entity.Context)
                       + " ," + Nullable(entity.Replace) + ")";
            return q;
        }

        protected override string GetUpdateQuery(Substitution entity) {
            string q = "UPDATE [Substitution]"
                       + "   SET [Alias] = "+Nullable(entity.Alias)
                       + "      ,[Value] = " + Nullable(entity.Value)
                       + "      ,[Replace] = " + Nullable(entity.Replace)
                       + "      ,[Condition] = " + Nullable(entity.Condition)
                       + "      ,[Context] = " + Nullable(entity.Context)
                       + " WHERE id=" + entity.Id;
            return q;
        }

        protected override void FillEntity(Substitution entity, DataRow myDataRow) {
            base.FillEntity(entity, myDataRow);
            entity.Alias = (string) myDataRow["Alias"];
            entity.Condition = myDataRow["Condition"] as string;
            entity.Replace = (string)myDataRow["Replace"];
            entity.Value = (string)myDataRow["Value"];
            entity.Context = myDataRow["Context"] as string;
        }
    }
}